package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.table.GDDevotionTable;
import org.gdstash.ui.table.GDDevotionTableModel;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class GDCharDevotionPane extends AdjustablePanel {
    private GDDevotionTableModel model;
    private GDDevotionTable table;

    public GDCharDevotionPane() {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final JScrollPane scroll = new JScrollPane(this.table);
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(scroll));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(scroll));
        layout.setVerticalGroup(vGroup);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntTable = UIManager.getDefaults().getFont("TableHeader.font");
        if (fntTable == null) {
            fntTable = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntTable = fntTable.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_DEVOTION_SKILLS"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.table == null) {
            (this.table = new GDDevotionTable()).setPreferredScrollableViewportSize(new Dimension(GDStashFrame.iniConfig.sectUI.fontSize * 25, GDStashFrame.iniConfig.sectUI.fontSize * 15));
            this.setChar(null);
        }
        this.table.getTableHeader().setFont(fntTable);
        this.table.setFont(fntLabel);
    }

    public void setChar(final GDChar gdc) {
        this.model = new GDDevotionTableModel(gdc);
        this.table.setModel(this.model);
        this.table.setSelectionMode(0);
    }

    public void updateChar(final GDChar gdc) {
        if (gdc == null) {
            return;
        }
        final GDDevotionTableModel model = (GDDevotionTableModel) this.table.getModel();
        final GDChar.SkillInfo[] devotions = model.getDevotions();
        gdc.setSkillLevel(devotions);
    }
}
